$(document).ready(function(){

	var _SCP_BORDER_HEIGHT = 3;
	function getnearby() {
		
		var cityname = document.getElementById("citythree").options[document.getElementById("citythree").selectedIndex].text;
		
		var searchname = $("#heldareas").val();
		$.get("/index.php",{ac:"public",at:"searchnearby",citycode:encodeURIComponent(cityname),searchname:encodeURIComponent(searchname),ran:Math.random()},
		function(data){
		  var data = eval("("+data+")");
		  var s = '<dl><dt>选择下面地标 ↑↓选择</dt>';
		  for(var i=0;i<data.length;i++) {
			  s += '<dd><a href="#body" name="'+data[i].Lng+','+data[i].Lat+'_'+data[i].Name+'" title="'+data[i].Name+'" onclick="setNearValue(\''+data[i].Lng+','+data[i].Lat+'\',\''+data[i].Name+'\');" hidefocus="true">'+data[i].Name+'</a></dd>';
		  }
		  s += '</dl>';
		  $("#autoCompleteNearby").html('');
		  $("#autoCompleteNearby").html(s);
		  var offset = $("#heldareas").offset();
		  $(".heldareas").css({"left":offset.left,"top":offset.top+$("#heldareas").height()+_SCP_BORDER_HEIGHT});
		  $(".heldareas").show();
		}); 
	}
	
		//-----------------------------------------------
	//绑定键盘上下键控制自动完成
	var currentLine=-1;
	var ncurrentLine=-1;
	$(document).keydown(function(event){
		e = event ? event : (window.event ? window.event : null); 
		var isnearby = $(".heldareas").css('display');
		if(isnearby == "block") {
			switch(e.keyCode)    
			{
				case 38: //上方向键
					ncurrentLine--;
					changeItem(e.keyCode); 
					break; 
				case 40: //下方向键
					ncurrentLine++; 
					changeItem(e.keyCode); 
					break; 
				default: 
					break;
			}
		}

		$("#autoCompleteNearby dd > a").mouseover(function(){
			$("#autoCompleteNearby dd > a").removeClass("active");
		});
		function changeItem(code)
		{
				$("#autoCompleteNearby dd > a").removeClass("active");
				var len = $("#autoCompleteNearby dd").length;
				if(code == 38) {
					if(ncurrentLine == -1) {
						$("#heldareas").focus();
					} else if(ncurrentLine < -1) {
						ncurrentLine=len;
						var nearvalue = $("#autoCompleteNearby a").get(ncurrentLine).name.split('_');
						$("#heldareas").val(nearvalue[1]);
						$("#coordinate").val(nearvalue[0]);
						$("#autoCompleteNearby a").get(ncurrentLine).className="active";
						$("#autoCompleteNearby a").get(ncurrentLine).focus();
					} else {
						var nearvalue = $("#autoCompleteNearby a").get(ncurrentLine).name.split('_');
						$("#heldareas").val(nearvalue[1]);
						$("#coordinate").val(nearvalue[0]);
						$("#autoCompleteNearby a").get(ncurrentLine).className="active";
						$("#autoCompleteNearby a").get(ncurrentLine).focus();
					}
				}
				if(code == 40) {
					if(ncurrentLine<-1) ncurrentLine=len;
					if(ncurrentLine >= len) {
						ncurrentLine = -1;
						$("#heldareas").focus();
					} else {
						var nearvalue = $("#autoCompleteNearby a").get(ncurrentLine).name.split('_');
						$("#heldareas").val(nearvalue[1]);
						$("#coordinate").val(nearvalue[0]);
						$("#autoCompleteNearby a").get(ncurrentLine).className="active";
						$("#autoCompleteNearby a").get(ncurrentLine).focus();
					}
				}
			}
	});
	//--------------------------------------------------------------
	//根据鼠标坐标关闭自动完成提示框
	$(document).click(function(e){
		e= e || window.event; 
		var mousePos = mouseCoords(e); 
		var offset = $("#citythree").offset();
		var noffset = $("#heldareas").offset();
		var isnearby = $(".heldareas").css('display');
		if(isnearby == 'block') {
			if(mousePos.x < noffset.left || mousePos.x > (noffset.left+$("#heldareas").width()) || mousePos.y < noffset.top || mousePos.y > (noffset.top + $("#heldareas").height() + $("#autoCompleteNearby").height())) {
				$("#autoCompleteNearby").hide();
			}	
		}
	});
	//获取鼠标坐标
	function mouseCoords(ev) 
	{ 
		if(ev.pageX || ev.pageY){ 
			return {x:ev.pageX, y:ev.pageY}; 
		} 
		return { 
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
			y:ev.clientY + document.body.scrollTop - document.body.clientTop 
		}; 
	}
	
	//附近标志性坐标
	$("#heldareas").keyup(function(event) {
		var heldareas = $("#heldareas").val();
		if(heldareas) {
			if (event.keyCode != 13 && event.keyCode != 9 && event.keyCode != 20 && event.keyCode != 16 && event.keyCode != 17 && event.keyCode !=38 &&  event.keyCode !=40) {
				getnearby();
			}
		}
	});
	$("#heldareas").blur(function() {
		if($("#citythree").val() != '' && $("#heldareas").val() != '' && $("#coordinate").val() == '') {
			getnearby();
		}
	});
	
});

function setNearValue(coord,name){
	$("#heldareas").val(name);
	$("#lnglat").val(coord);
	$("#autoCompleteNearby").hide();
}